package CoreJavaKnow;
public class PairTest1 {

	public static void main(String[] args) {
		String[] strs = { "Mary", "had", "abc", "little", "lamb" };
		Pair<String> minMax = ArrayAlg.minMax(strs);
		System.out.println(minMax.getFirst() + "," + minMax.getSecond());

		String middle = ArrayAlg.<String> getMiddle(strs);
		System.out.println(middle);

	}
}

class ArrayAlg {

	public static <T extends Comparable> Pair<T> minMax(T[] a) {
		if (a == null || a.length == 0)
			return null;
		T min = a[0];
		T max = a[0];
		for (int i = 1; i < a.length; i++) {
			if (min.compareTo(a[i]) > 0)
				min = a[i];
			if (max.compareTo(a[i]) < 0)
				max = a[i];
		}
		return new Pair<T>(min, max);
	}

	public static <T> T getMiddle(T[] a) {
		return a[a.length / 2];
	}

	public static <T extends Comparable> T min(T[] a) // almost correct
	{
		if (a == null || a.length == 0)
			return null;
		T smallest = a[0];
		for (int i = 1; i < a.length; i++)
			if (smallest.compareTo(a[i]) > 0)
				smallest = a[i];
		return smallest;
	}

}
